<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('goods-tag');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回标签列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存标签'); ?></button>
            <?php echo $this->HelpUrl('goodsTag'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab"><?php echo $this->translate('基本信息'); ?></a></li>
                        <?php if($this->tagInfo) { ?>
                            <li><a href="#tab_2" data-toggle="tab"><?php echo $this->translate('标签商品'); ?></a></li>
                        <?php } ?>
                    </ul>
                    <div class="tab-content">
                        <?php echo $this->partial('layout/messages'); ?>
                        <div class="tab-pane active" id="tab_1">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('标签分组'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-3">
                                        <?php echo $this->formElement($form->get('tagGroupId')); ?>
                                        <?php echo $this->formElementErrors($form->get('tagGroupId'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('标签名称'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-4">
                                        <?php echo $this->formElement($form->get('tagName')); ?>
                                        <?php echo $this->formElementErrors($form->get('tagName'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('排序'); ?></label>
                                    <div class="col-sm-2">
                                        <?php echo $this->formElement($form->get('tagSort')); ?>
                                        <?php echo $this->formElementErrors($form->get('tagSort'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('颜色设置'); ?></label>
                                    <div class="col-sm-2 inline">
                                        <?php echo $this->formElement($form->get('tagStr')); ?>
                                        <?php echo $this->formElementErrors($form->get('tagStr'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php if($this->tagInfo) { ?>
                        <div class="tab-pane" id="tab_2">
                            <p class="text-right">
                                <button type="button" class="btn btn-success btn-sm"  data-toggle="modal" onclick="addTagGoods();"><i class="fa fa-plus"></i> <?php echo $this->translate('点击添加商品')?></button>
                            </p>

                            <div class="box-body table-responsive no-padding" id="goodsListBody">
                                <table class="table shop-table">
                                    <thead>
                                    <tr class="shop-bg-thead">
                                        <th width="5%">ID</th>
                                        <th width="45%"><?php echo $this->translate('商品名称'); ?></th>
                                        <th width="15%"><?php echo $this->translate('商品状态'); ?></th>
                                        <th width="15%"><?php echo $this->translate('操作'); ?></th>
                                    </tr>
                                    </thead>
                                    <tbody id="insert-goods">
                                    <?php if(isset($this->goodsList)) foreach ($this->goodsList as $goodsValue) { ?>
                                        <tr id="in-goods-<?php echo $goodsValue[0]->getInTagId(); ?>">
                                            <td><?php echo $goodsValue[0]->getGoodsId(); ?></td>
                                            <td><?php echo $this->escapeHtml($goodsValue['goodsName']); ?></td>
                                            <td><?php echo $this->goodsHelper()->goodsState($goodsValue['goodsState']); ?></td>
                                            <td>
                                                <a href="javascript:;" onclick="deleteNotConfirm( '<?php echo $this->url('goods-tag', ['action'=>'deleteTagGoods', 'id'=>$goodsValue[0]->getInTagId()]); ?>', 'in-goods-<?php echo $goodsValue[0]->getInTagId(); ?>');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                    <tfoot>
                                    <tr>
                                        <td colspan="4">
                                            <?php echo $this->paginationControl($this->goodsList,
                                                'Sliding',
                                                'layout/ajaxPage',
                                                ['route' => 'goods-tag', 'params' => ['action' => 'ajaxTagGoods', 'id' => $this->tagInfo->getTagId()], 'showDivId' => 'goodsListBody']); ?>
                                        </td>
                                    </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                        <?php } ?>

                    </div>
                </div>
            </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

    <div class="modal fade" id="modalDefault">
        <div class="modal-dialog" style="width: 800px;" id="addGoodsListBody">

        </div>
    </div>

</div>
<script>var imageUrl='<?php echo $this->basePath('js/plugins/iColorPicker/color.png'); ?>';</script>
<script src="<?php echo $this->basePath('js/plugins/iColorPicker/iColorPicker.js'); ?>"></script>
<script>
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                tagGroupId: {
                    required: true
                },
                tagName: {
                    required: true
                },
                tagSort: {
                    required: true,
                    number: true,
                    min: 1
                }
            },
            messages: {
                tagGroupId: {
                    required: "<?php echo $this->translate('请选择标签分组!'); ?>"
                },
                tagName: {
                    required: "<?php echo $this->translate('标签名称不能为空!'); ?>"
                },
                tagSort: {
                    required: "<?php echo $this->translate('排序不能为空!'); ?>",
                    number: "<?php echo $this->translate('排序必须为数字!'); ?>",
                    min: "<?php echo $this->translate('排序最小为1!'); ?>"
                }
            }
        });
    });

    <?php if($this->tagInfo) { ?>
    function addTagGoods() {
        $('#modalDefault').modal('show');
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>', 'addGoodsListBody');
    }
    function searchGoods() {
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>?searchGoodsName='+$('#searchGoodsName').val(), 'addGoodsListBody');
    }
    function addSelectGoods(goodsId) {
        if(goodsId == '') {
            shopMessage('<?php echo $this->translate('请填写需要添加的商品名称!')?>');
        } else {
            let insertState = true;
            $("input[name='goodsId[]']").each(function () {
                if($(this).val() == goodsId) {
                    insertState = false;
                }
            });
            if(!insertState) {
                return false;
            }
            $.post("<?php echo $this->url('goods-tag', ['action' => 'addTagGoods']);?>", {goodsId: goodsId, tagId:<?php echo $this->tagInfo->getTagId(); ?>}, function (data) {
                if(data.state == 'ok') {
                    let htmlStr = '<tr id="in-goods-'+data.result.inTagId+'">';
                    htmlStr += '<td>'+data.result.goodsId+'</td>';
                    htmlStr += '<td>'+data.result.goodsName+'</td>';
                    htmlStr += '<td>'+data.result.goodsState+'</td>';
                    htmlStr += '<td><a href="javascript:;" onclick="deleteNotConfirm(\'<?php echo $this->url('goods-tag', ['action'=>'deleteTagGoods']); ?>/'+data.result.inTagId+'\', \'in-goods-'+data.result.inTagId+'\');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a></td>';
                    htmlStr += '</tr>';

                    $('#insert-goods').prepend(htmlStr);
                } else {
                    shopMessage('<?php echo  $this->translate('该商品已经添加!')?>');
                }
            }, 'json');
        }
    }
    function addMoreSelectGoods() {
        $("input[name='selectGoodsId[]']").each(function () {
            if(this.checked) {
                addSelectGoods(this.value);
            }
        });
        $('#modalDefault').modal('hide');
    }
    <?php } ?>
</script>
